package com.vessel.decoration.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.vessel.decoration.app.data.entity.ArtisanInfoResponse;
import com.vessel.decoration.mvp.contract.ArtisanContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class ArtisanPresenter extends BasePresenter<ArtisanContract.Model, ArtisanContract.View> {
    private RxErrorHandler mErrorHandler;
    private AppManager mAppManager;
    private Application mApplication;
    private int mArtisanId = -1;

    @Inject
    public ArtisanPresenter(ArtisanContract.Model model, ArtisanContract.View rootView, RxErrorHandler handler
            , AppManager appManager, Application application) {
        super(model, rootView);
        this.mApplication = application;
        this.mErrorHandler = handler;
        this.mAppManager = appManager;
    }

    public void getArtisanDetail(int artisanId) {
        mModel.queryArtisanInfo(artisanId)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<ArtisanInfoResponse>(mErrorHandler) {
                    @Override
                    public void onNext(ArtisanInfoResponse artisanInfoResponse) {
                        mArtisanId = artisanInfoResponse.getArtisanId();
                        mRootView.setData2View(artisanInfoResponse);
                    }
                });
    }

    public void collect(boolean shoucangStatus) {
        int type = -1;
        if (shoucangStatus) {
            type = 0;
        } else {
            type = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("artisanIds", mArtisanId);
        map.put("type", type);
        mModel.collectOrCancel(map)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<Boolean>(mErrorHandler) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        mRootView.updateStatus();
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}
